var lastId = -1;
var usedIds = new Array();
var usedPIds = new Array();

function showTodoBox(id){
	if(lastId != -1 && lastId != -100){
		$('#project_li_'+lastId).removeClass('selected');
		$('#todo_box_'+lastId).hide();
	} else if(lastId == -100){
		$('#project_li_all').removeClass('selected');
		$('#todo_box_all').hide();
	}
	
	if(id==-100){
		$('#project_li_all').addClass('selected');
		$('#todo_box_all').show();
	} else {
		$('#project_li_'+id).addClass('selected');
		$('#todo_box_'+id).show();
	}
	lastId = id;
}

function loadTodos(id, page){
	if(lastId != -1 && lastId != -100){
		$('#project_li_'+lastId).removeClass('selected');
		$('#todo_box_'+lastId).hide();
	} else if(lastId == -100){
		$('#project_li_all').removeClass('selected');
		$('#todo_box_all').hide();
	}
	
	if(id==-100){
		$('#project_li_all').addClass('selected');
		$('#todo_box_all').show();
	} else {
		$('#project_li_'+id).addClass('selected');
		$('#todo_box_'+id).show();
	}
	loadTodosFromServer(id, page);
	lastId = id;
}

function loadTodosFromServer(id, page){
	if(id==-100){
		$('#todo_list').html(loading_img);
		$.ajax({
		   type: "POST",
		   url: "connector.php",
		   data: 'action=getAllTodos&sID='+aSID+'&page='+page,
		   success: function(msg) {
				if(msg == 'Error:102') document.location = 'logout.php?action=force';
				$('#todo_list').html(msg);
				updateActionBoxes();
		   }
		});
	} else {
		$('#todo_list').html(loading_img);
		$.ajax({
		   type: "POST",
		   url: "connector.php",
		   data: 'action=getTodos&lID='+id+'&sID='+aSID+'&page='+page,
		   success: function(msg) {
				if(msg == 'Error:102') document.location = 'logout.php?action=force';
				$('#todo_list').html(msg);
				updateActionBoxes();
		   }
		});
	}
}

function showAddUser(){
	$('#user_options').html(loading_img);
	$.ajax({
	   type: "POST",
	   url: "connector.php",
	   data: 'action=getUsers&sID='+aSID,
	   success: function(msg) {
			$('#user_options').html(msg);
	   }
	});
	showPopUp('select_user');
}

function addUser(id) {
	var user_id = $('#ajax_user').val();
	if($.inArray(parseInt(user_id), usedIds) == -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=addUser&tID='+id+'&uID='+user_id+'&sID='+aSID,
			   success: function(msg) {
					if(msg == 'Error:102') {
						document.location = 'logout.php?action=force';
					} else {
						showMessage(addUSuccess);
						$('#all_users').append(newUser[0]+id+newUser[1]+user_id+newUser[2]+msg+newUser[3]+user_id+newUser[4]+id+newUser[5]);
						usedIds.push(user_id);
					}
			   }
			});
	} else {
		showMessage(alreadyAdded);
	}
	closePopUp();
}

function deleteUser(id, tId){
	if($.inArray(parseInt(id), usedIds) > -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=deleteUser&tID='+tId+'&uID='+id+'&sID='+aSID,
			   success: function(msg) {
					if(msg == 'Error:102') {
						document.location = 'logout.php?action=force';
					}else if(msg == 'ok'){
						showMessage(deleteUSuccess);
						$('#todo_user_'+tId+'_'+id).remove();
						usedIds = jQuery.grep(usedIds, function(value) { return value != id;});
					} else {
						alert(msg);
					}
			   }
			});
	}
}

function showAddProject(){
	$('#project_options').html(loading_img);
	$.ajax({
	   type: "POST",
	   url: "connector.php",
	   data: 'action=getProjects&sID='+aSID,
	   success: function(msg) {
			$('#project_options').html(msg);
	   }
	});
	showPopUp('select_project');
}

function addProject(id) {
	var project_id = $('#ajax_project').val();
	if($.inArray(parseInt(project_id), usedPIds) == -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=addProject&sID='+aSID+'&pID='+project_id+'&tID='+id,
			   success: function(msg) {
						if(msg == 'Error:102') {
							document.location = 'logout.php?action=force';
						}else {
							showMessage(addPSuccess);
							$('#all_projects').append(newProject[0]+id+newProject[1]+project_id+newProject[2]+msg+newProject[3]+project_id+newProject[4]+id+newProject[5]);
							usedPIds.push(project_id);
						}
			   }
			});
	}else {
		showMessage(alreadyAddedP);
	}
	closePopUp();
}

function deleteProject(id, tId){
	if($.inArray(parseInt(id), usedPIds) > -1){
		$.ajax({
			   type: "POST",
			   url: "connector.php",
			   data: 'action=deleteProject&tID='+tId+'&pID='+id+'&sID='+aSID,
			   success: function(msg) {
					if(msg == 'Error:102') {
						document.location = 'logout.php?action=force';
					}else if(msg == 'ok'){
						showMessage(deletePSuccess);
						$('#todo_project_'+tId+'_'+id).remove();
						usedPIds = jQuery.grep(usedPIds, function(value) { return value != id;});
					} else {
						alert(msg);
					}
			   }
			});
	}
}